﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }


    protected void Application1_Click(object sender, EventArgs e)
    {
        PostByWebRequestNQU();
    }


    protected void Application2_Click(object sender, EventArgs e)
    {
        PostByWebRequest2();
    }
    private void PostByWebRequest()
    {
        try
        {
            CookieContainer cookieContainer = new CookieContainer();

            ///////////////////////////////////////////////////
            // 1.打開 MyLogin.aspx 頁面，獲得 GetVeiwState & EventValidation
            ///////////////////////////////////////////////////                
            // 設置打開頁面的參數
            string URI = "http://localhost:52641/WebTest/MyLogin.aspx";
            HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
            request.Method = "GET";
            request.KeepAlive = false;

            // 接收返回的頁面
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            System.IO.Stream responseStream = response.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            string srcString = reader.ReadToEnd();

            // 獲取頁面的 VeiwState                
            string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
            int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
            int j = srcString.IndexOf("\"", i);
            string viewState = srcString.Substring(i, j - i);
            
            // 獲取頁面的 EventValidation                
            string eventValidationFlag = "id=\"__EVENTVALIDATION\" value=\"";
            i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
            j = srcString.IndexOf("\"", i);
            string eventValidation = srcString.Substring(i, j - i);

            ///////////////////////////////////////////////////
            // 2.自動填充並提交 MyLogin.aspx 頁面
            ///////////////////////////////////////////////////
            // 提交按鈕的文本
            string submitButton = "登錄";

            // 用戶名和密碼
            string userName = "1";
            string password = "1";

            // 將文本轉換成 URL 編碼字符串
            viewState = System.Web.HttpUtility.UrlEncode(viewState);
            eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
            submitButton = System.Web.HttpUtility.UrlEncode(submitButton);

            // 要提交的字符串數據。格式形如:user=uesr1&password=123
            string formatString = "userName={0}&password={1}&loginButton={2}&__VIEWSTATE={3}&__EVENTVALIDATION={4}";
            string postString = string.Format(formatString, userName, password, submitButton, viewState, eventValidation);

            // 將提交的字符串數據轉換成字節數組
            byte[] postData = Encoding.ASCII.GetBytes(postString);

            // 設置提交的相關參數
            request = WebRequest.Create(URI) as HttpWebRequest;
            request.Method = "POST";
            request.KeepAlive = false;
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookieContainer;
            request.ContentLength = postData.Length;

            // 提交請求數據
            System.IO.Stream outputStream = request.GetRequestStream();
            outputStream.Write(postData, 0, postData.Length);
            outputStream.Close();

            // 接收返回的頁面
            response = request.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
            srcString = reader.ReadToEnd();

            ///////////////////////////////////////////////////
            // 3.打開 Default.aspx 頁面
            ///////////////////////////////////////////////////
            // 設置打開頁面的參數
            URI = "http://localhost:52641/WebTest/Verification1.aspx";
            request = WebRequest.Create(URI) as HttpWebRequest;
            request.Method = "GET";
            request.KeepAlive = false;
            request.CookieContainer = cookieContainer;

            // 接收返回的頁面
            response = request.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            srcString = reader.ReadToEnd();


            //divTest.InnerHtml = srcString;//

            ///////////////////////////////////////////////////
            // 4.分析返回的頁面
            ///////////////////////////////////////////////////
            // ...... ......
        }
        catch (WebException we)
        {
            string msg = we.Message;
        }

    }
    private void PostByWebRequest2()
    {
        try
        {
            CookieContainer cookieContainer = new CookieContainer();

            ///////////////////////////////////////////////////
            // 1.打開 MyLogin.aspx 頁面，獲得 GetVeiwState & EventValidation
            ///////////////////////////////////////////////////                
            // 設置打開頁面的參數
            string URI = "http://localhost:53730/WebTest2/MyLogin2.aspx";
            HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
            request.Method = "GET";
            request.KeepAlive = false;

            // 接收返回的頁面
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            System.IO.Stream responseStream = response.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            string srcString = reader.ReadToEnd();

            // 獲取頁面的 VeiwState                
            string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
            int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
            int j = srcString.IndexOf("\"", i);
            string viewState = srcString.Substring(i, j - i);

            // 獲取頁面的 EventValidation                
            string eventValidationFlag = "id=\"__EVENTVALIDATION\" value=\"";
            i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
            j = srcString.IndexOf("\"", i);
            string eventValidation = srcString.Substring(i, j - i);

            ///////////////////////////////////////////////////
            // 2.自動填充並提交 MyLogin.aspx 頁面
            ///////////////////////////////////////////////////
            // 提交按鈕的文本
            string submitButton = "登錄";

            // 用戶名和密碼
            string userName = "2";
            string password = "2";

            // 將文本轉換成 URL 編碼字符串
            viewState = System.Web.HttpUtility.UrlEncode(viewState);
            eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
            submitButton = System.Web.HttpUtility.UrlEncode(submitButton);

            // 要提交的字符串數據。格式形如:user=uesr1&password=123
            string formatString = "userName={0}&password={1}&loginButton={2}&__VIEWSTATE={3}&__EVENTVALIDATION={4}";
            string postString = string.Format(formatString, userName, password, submitButton, viewState, eventValidation);

            // 將提交的字符串數據轉換成字節數組
            byte[] postData = Encoding.ASCII.GetBytes(postString);

            // 設置提交的相關參數
            request = WebRequest.Create(URI) as HttpWebRequest;
            request.Method = "POST";
            request.KeepAlive = false;
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookieContainer;
            request.ContentLength = postData.Length;
            request.UserAgent = @"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";


            // 提交請求數據
            System.IO.Stream outputStream = request.GetRequestStream();
            outputStream.Write(postData, 0, postData.Length);
            outputStream.Close();

            // 接收返回的頁面
            response = request.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
            srcString = reader.ReadToEnd();

            ///////////////////////////////////////////////////
            // 3.打開 Default.aspx 頁面
            ///////////////////////////////////////////////////
            // 設置打開頁面的參數
            URI = "http://localhost:53730/WebTest2/Verification2.aspx";
            request = WebRequest.Create(URI) as HttpWebRequest;
            request.Method = "GET";
            request.KeepAlive = false;
            request.CookieContainer = cookieContainer;

            // 接收返回的頁面
            response = request.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            srcString = reader.ReadToEnd();

            divTest.InnerHtml = srcString;


            ///////////////////////////////////////////////////
            // 4.分析返回的頁面
            ///////////////////////////////////////////////////
            // ...... ......
        }
        catch (WebException we)
        {
            string msg = we.Message;
        }

 }
    private void PostByWebRequestNQU()
    {
        try
        {
            CookieContainer cookieContainer = new CookieContainer();

            ///////////////////////////////////////////////////
            // 1.打開 MyLogin.aspx 頁面，獲得 GetVeiwState & EventValidation
            ///////////////////////////////////////////////////                
            // 設置打開頁面的參數
            string URI = "http://ecampus.nqu.edu.tw/eCampus3P/Learn/LoginPage2/product_login.aspx";
            HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
            request.Method = "GET";
            request.KeepAlive = false;

            // 接收返回的頁面
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            System.IO.Stream responseStream = response.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            string srcString = reader.ReadToEnd();

            // 獲取頁面的 VeiwState                
            string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
            int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
            int j = srcString.IndexOf("\"", i);
            string viewState = srcString.Substring(i, j - i);

            // 獲取頁面的 __EVENTTARGET                
            string EVENTTARGETFlag = "id=\"__EVENTTARGET\" value=\"";
            i = srcString.IndexOf(EVENTTARGETFlag) + EVENTTARGETFlag.Length;
            j = srcString.IndexOf("\"", i);
            string eventtarget = srcString.Substring(i, j - i);

            // 獲取頁面的 __EVENTARGUMENT                
            string EVENTARGUMENTFlag = "id=\"__EVENTTARGET\" value=\"";
            i = srcString.IndexOf(EVENTARGUMENTFlag) + EVENTARGUMENTFlag.Length;
            j = srcString.IndexOf("\"", i);
            string eventargument = srcString.Substring(i, j - i);



            ///////////////////////////////////////////////////
            // 2.自動填充並提交 MyLogin.aspx 頁面
            ///////////////////////////////////////////////////
            // 提交按鈕的文本
            string btnLogin1 = "43";
            string btnLogin2 = "43";

            // 用戶名和密碼
            string userName = "109910607";
            string password = "2202";

            // 將文本轉換成 URL 編碼字符串
            viewState = System.Web.HttpUtility.UrlEncode(viewState);
            eventtarget = System.Web.HttpUtility.UrlEncode(eventtarget);
            eventargument = System.Web.HttpUtility.UrlEncode(eventargument);
            btnLogin1 = System.Web.HttpUtility.UrlEncode(btnLogin1);
            btnLogin2 = System.Web.HttpUtility.UrlEncode(btnLogin2);

            // 要提交的字符串數據。格式形如:user=uesr1&password=123
            string formatString = "txtLoginId={0}&txtLoginPwd={1}&btnLogin.x={2}&btnLogin.y={3}&__VIEWSTATE={4}&__EVENTTARGET={5}&__EVENTARGUMENT={6}";
            string postString = string.Format(formatString, userName, password, btnLogin1, btnLogin2, viewState, eventtarget, eventargument);

            // 將提交的字符串數據轉換成字節數組
            byte[] postData = Encoding.ASCII.GetBytes(postString);

            // 設置提交的相關參數
            request = WebRequest.Create(URI) as HttpWebRequest;
            request.Method = "POST";
            request.KeepAlive = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookieContainer;
            request.ContentLength = postData.Length;

            // 提交請求數據
            System.IO.Stream outputStream = request.GetRequestStream();
            outputStream.Write(postData, 0, postData.Length);
            outputStream.Close();

            // 接收返回的頁面
            response = request.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
            srcString = reader.ReadToEnd();


            ///////////////////////////////////////////////////
            // 3.打開 Default.aspx 頁面
            ///////////////////////////////////////////////////
            // 設置打開頁面的參數
            URI = "http://ecampus.nqu.edu.tw/eCampus3P/Learn/enter_course_index.aspx";
            request = WebRequest.Create(URI) as HttpWebRequest;
            request.Method = "GET";
            request.KeepAlive = false;
            request.CookieContainer = cookieContainer;

            

          // 接收返回的頁面
            response = request.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
            srcString = reader.ReadToEnd();

            frmMain.Attributes["src"] = URI;
            //divTest.InnerHtml = srcString;


            
            ///////////////////////////////////////////////////
            // 4.分析返回的頁面
            ///////////////////////////////////////////////////
            // ...... ......
        }
        catch (WebException we)
        {
            string msg = we.Message;
        }

    }


}